1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package com.sun.security.auth;
27
28 import java.io.*;
29 import java.lang.RuntimePermission;
30 import java.lang.reflect.*;
31 import java.net.MalformedURLException;
32 import java.net.URL;
33 import java.util.*;
34
35 import java.security.AccessController;
36 import java.security.CodeSource;
37 import java.security.KeyStore;
38 import java.security.KeyStoreException;
39 import java.security.Permission;
40 import java.security.Permissions;
41 import java.security.PermissionCollection;
42 import java.security.Principal;
43 import java.security.UnresolvedPermission;
44 import java.security.Security;
45 import java.security.cert.Certificate;
46 import java.security.cert.X509Certificate;
47
48 import javax.security.auth.Subject;
49 import javax.security.auth.PrivateCredentialPermission;
50
51 import sun.security.util.PropertyExpander;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242 @Deprecated
243 public class PolicyFile extends javax.security.auth.Policy {
244
245 static final java.util.ResourceBundle rb =
246 java.security.AccessController.doPrivileged
247 (new java.security.PrivilegedAction<java.util.ResourceBundle>() {
248 public java.util.ResourceBundle run() {
249 return (java.util.ResourceBundle.getBundle
250 ("sun.security.util.AuthResources"));
251 }
252 });
253
254
255 private static final sun.security.util.Debug debug =
256 sun.security.util.Debug.getInstance("policy", "\t[Auth Policy]");
257
258 private static final String AUTH_POLICY = "java.security.auth.policy";
259 private static final String SECURITY_MANAGER = "java.security.manager";
260 private static final String AUTH_POLICY_URL = "auth.policy.url.";
261
262 private Vector<PolicyEntry> policyEntries;
263 private Hashtable aliasMapping;
264
265 private boolean initialized = false;
266
267 private boolean expandProperties = true;
268 private boolean ignoreIdentityScope = true;
269
270
271
272 private static final Class[] PARAMS = { String.class, String.class};
273
274
275
276
277
278 public PolicyFile() {
279
280
281 String prop = System.getProperty(AUTH_POLICY);
282
283 if (prop == null) {
284 prop = System.getProperty(SECURITY_MANAGER);
285 }
286 if (prop != null)
287 init();
288 }
289
290 private synchronized void init() {
291
292 if (initialized)
293 return;
294
295 policyEntries = new Vector<PolicyEntry>();
296 aliasMapping = new Hashtable(11);
297
298 initPolicyFile();
299 initialized = true;
300 }
301
302
303
304
305
306
307
308
309
310 public synchronized void refresh()
311 {
312
313 java.lang.SecurityManager sm = System.getSecurityManager();
314 if (sm != null) {
315 sm.checkPermission(new javax.security.auth.AuthPermission
316 ("refreshPolicy"));
317 }
318
319
320
321
322
323
324
325
326
327
328
329
330 initialized = false;
331 java.security.AccessController.doPrivileged
332 (new java.security.PrivilegedAction<Void>() {
333 public Void run() {
334 init();
335 return null;
336 }
337 });
338 }
339
340 private KeyStore initKeyStore(URL policyUrl, String keyStoreName,
341 String keyStoreType) {
342 if (keyStoreName != null) {
343 try {
344
345
346
347
348 URL keyStoreUrl = null;
349 try {
350 keyStoreUrl = new URL(keyStoreName);
351
352 } catch (java.net.MalformedURLException e) {
353
354 keyStoreUrl = new URL(policyUrl, keyStoreName);
355 }
356
357 if (debug != null) {
358 debug.println("reading keystore"+keyStoreUrl);
359 }
360
361 InputStream inStream =
362 new BufferedInputStream(getInputStream(keyStoreUrl));
363
364 KeyStore ks;
365 if (keyStoreType != null)
366 ks = KeyStore.getInstance(keyStoreType);
367 else
368 ks = KeyStore.getInstance(KeyStore.getDefaultType());
369 ks.load(inStream, null);
370 inStream.close();
371 return ks;
372 } catch (Exception e) {
373
374 if (debug != null) {
375 e.printStackTrace();
376 }
377 return null;
378 }
379 }
380 return null;
381 }
382
383 private void initPolicyFile() {
384
385 String prop = Security.getProperty("policy.expandProperties");
386
387 if (prop != null) expandProperties = prop.equalsIgnoreCase("true");
388
389 String iscp = Security.getProperty("policy.ignoreIdentityScope");
390
391 if (iscp != null) ignoreIdentityScope = iscp.equalsIgnoreCase("true");
392
393 String allowSys = Security.getProperty("policy.allowSystemProperty");
394
395 if ((allowSys!=null) && allowSys.equalsIgnoreCase("true")) {
396
397 String extra_policy = System.getProperty(AUTH_POLICY);
398 if (extra_policy != null) {
399 boolean overrideAll = false;
400 if (extra_policy.startsWith("=")) {
401 overrideAll = true;
402 extra_policy = extra_policy.substring(1);
403 }
404 try {
405 extra_policy = PropertyExpander.expand(extra_policy);
406 URL policyURL;;
407 File policyFile = new File(extra_policy);
408 if (policyFile.exists()) {
409 policyURL =
410 new URL("file:" + policyFile.getCanonicalPath());
411 } else {
412 policyURL = new URL(extra_policy);
413 }
414 if (debug != null)
415 debug.println("reading "+policyURL);
416 init(policyURL);
417 } catch (Exception e) {
418
419 if (debug != null) {
420 debug.println("caught exception: "+e);
421 }
422
423 }
424 if (overrideAll) {
425 if (debug != null) {
426 debug.println("overriding other policies!");
427 }
428 return;
429 }
430 }
431 }
432
433 int n = 1;
434 boolean loaded_one = false;
435 String policy_url;
436
437 while ((policy_url = Security.getProperty(AUTH_POLICY_URL+n)) != null) {
438 try {
439 policy_url = PropertyExpander.expand(policy_url).replace
440 (File.separatorChar, '/');
441 if (debug != null)
442 debug.println("reading "+policy_url);
443 init(new URL(policy_url));
444 loaded_one = true;
445 } catch (Exception e) {
446 if (debug != null) {
447 debug.println("error reading policy "+e);
448 e.printStackTrace();
449 }
450
451 }
452 n++;
453 }
454
455 if (loaded_one == false) {
456
457 }
458 }
459
460
461
462
463
464
465 private boolean checkForTrustedIdentity(final Certificate cert) {
466
467
468 return false;
469 }
470
471
472
473
474
475
476
477 private void init(URL policy) {
478 PolicyParser pp = new PolicyParser(expandProperties);
479 try {
480 InputStreamReader isr
481 = new InputStreamReader(getInputStream(policy));
482 pp.read(isr);
483 isr.close();
484 KeyStore keyStore = initKeyStore(policy, pp.getKeyStoreUrl(),
485 pp.getKeyStoreType());
486 Enumeration<PolicyParser.GrantEntry> enum_ = pp.grantElements();
487 while (enum_.hasMoreElements()) {
488 PolicyParser.GrantEntry ge = enum_.nextElement();
489 addGrantEntry(ge, keyStore);
490 }
491 } catch (PolicyParser.ParsingException pe) {
492 System.err.println(AUTH_POLICY +
493 rb.getString(".error.parsing.") + policy);
494 System.err.println(AUTH_POLICY +
495 rb.getString("COLON") +
496 pe.getMessage());
497 if (debug != null)
498 pe.printStackTrace();
499
500 } catch (Exception e) {
501 if (debug != null) {
502 debug.println("error parsing "+policy);
503 debug.println(e.toString());
504 e.printStackTrace();
505 }
506 }
507 }
508
509
510
511
512
513
514
515
516 private InputStream getInputStream(URL url) throws IOException {
517 if ("file".equals(url.getProtocol())) {
518 String path = url.getFile().replace('/', File.separatorChar);
519 return new FileInputStream(path);
520 } else {
521 return url.openStream();
522 }
523 }
524
525
526
527
528
529
530 CodeSource getCodeSource(PolicyParser.GrantEntry ge, KeyStore keyStore)
531 throws java.net.MalformedURLException
532 {
533 Certificate[] certs = null;
534 if (ge.signedBy != null) {
535 certs = getCertificates(keyStore, ge.signedBy);
536 if (certs == null) {
537
538
539 if (debug != null) {
540 debug.println(" no certs for alias " +
541 ge.signedBy + ", ignoring.");
542 }
543 return null;
544 }
545 }
546
547 URL location;
548
549 if (ge.codeBase != null)
550 location = new URL(ge.codeBase);
551 else
552 location = null;
553
554 if (ge.principals == null || ge.principals.size() == 0) {
555 return (canonicalizeCodebase
556 (new CodeSource(location, certs),
557 false));
558 } else {
559 return (canonicalizeCodebase
560 (new SubjectCodeSource(null, ge.principals, location, certs),
561 false));
562 }
563 }
564
565
566
567
568 private void addGrantEntry(PolicyParser.GrantEntry ge,
569 KeyStore keyStore) {
570
571 if (debug != null) {
572 debug.println("Adding policy entry: ");
573 debug.println(" signedBy " + ge.signedBy);
574 debug.println(" codeBase " + ge.codeBase);
575 if (ge.principals != null && ge.principals.size() > 0) {
576 ListIterator<PolicyParser.PrincipalEntry> li =
577 ge.principals.listIterator();
578 while (li.hasNext()) {
579 PolicyParser.PrincipalEntry pppe = li.next();
580 debug.println(" " + pppe.principalClass +
581 " " + pppe.principalName);
582 }
583 }
584 debug.println();
585 }
586
587 try {
588 CodeSource codesource = getCodeSource(ge, keyStore);
589
590 if (codesource == null) return;
591
592 PolicyEntry entry = new PolicyEntry(codesource);
593 Enumeration<PolicyParser.PermissionEntry> enum_ =
594 ge.permissionElements();
595 while (enum_.hasMoreElements()) {
596 PolicyParser.PermissionEntry pe = enum_.nextElement();
597 try {
598
599 Permission perm;
600 if (pe.permission.equals
601 ("javax.security.auth.PrivateCredentialPermission") &&
602 pe.name.endsWith(" self")) {
603 perm = getInstance(pe.permission,
604 pe.name + " \"self\"",
605 pe.action);
606 } else {
607 perm = getInstance(pe.permission,
608 pe.name,
609 pe.action);
610 }
611 entry.add(perm);
612 if (debug != null) {
613 debug.println(" "+perm);
614 }
615 } catch (ClassNotFoundException cnfe) {
616 Certificate certs[];
617 if (pe.signedBy != null)
618 certs = getCertificates(keyStore, pe.signedBy);
619 else
620 certs = null;
621
622
623
624 if (certs != null || pe.signedBy == null) {
625 Permission perm = new UnresolvedPermission(
626 pe.permission,
627 pe.name,
628 pe.action,
629 certs);
630 entry.add(perm);
631 if (debug != null) {
632 debug.println(" "+perm);
633 }
634 }
635 } catch (java.lang.reflect.InvocationTargetException ite) {
636 System.err.println
637 (AUTH_POLICY +
638 rb.getString(".error.adding.Permission.") +
639 pe.permission +
640 rb.getString("SPACE") +
641 ite.getTargetException());
642 } catch (Exception e) {
643 System.err.println
644 (AUTH_POLICY +
645 rb.getString(".error.adding.Permission.") +
646 pe.permission +
647 rb.getString("SPACE") +
648 e);
649 }
650 }
651 policyEntries.addElement(entry);
652 } catch (Exception e) {
653 System.err.println
654 (AUTH_POLICY +
655 rb.getString(".error.adding.Entry.") +
656 ge +
657 rb.getString("SPACE") +
658 e);
659 }
660
661 if (debug != null)
662 debug.println();
663 }
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695 private static final Permission getInstance(String type,
696 String name,
697 String actions)
698 throws ClassNotFoundException,
699 InstantiationException,
700 IllegalAccessException,
701 NoSuchMethodException,
702 InvocationTargetException
703 {
704
705 Class pc = Class.forName(type);
706 Constructor c = pc.getConstructor(PARAMS);
707 return (Permission) c.newInstance(new Object[] { name, actions });
708 }
709
710
711
712
713 Certificate[] getCertificates(
714 KeyStore keyStore, String aliases) {
715
716 Vector<Certificate> vcerts = null;
717
718 StringTokenizer st = new StringTokenizer(aliases, ",");
719 int n = 0;
720
721 while (st.hasMoreTokens()) {
722 String alias = st.nextToken().trim();
723 n++;
724 Certificate cert = null;
725
726 cert = (Certificate) aliasMapping.get(alias);
727 if (cert == null && keyStore != null) {
728
729 try {
730 cert = keyStore.getCertificate(alias);
731 } catch (KeyStoreException kse) {
732
733
734 }
735 if (cert != null) {
736 aliasMapping.put(alias, cert);
737 aliasMapping.put(cert, alias);
738 }
739 }
740
741 if (cert != null) {
742 if (vcerts == null)
743 vcerts = new Vector<Certificate>();
744 vcerts.addElement(cert);
745 }
746 }
747
748
749 if (vcerts != null && n == vcerts.size()) {
750 Certificate[] certs = new Certificate[vcerts.size()];
751 vcerts.copyInto(certs);
752 return certs;
753 } else {
754 return null;
755 }
756 }
757
758
759
760
761
762
763
764 private final synchronized Enumeration<PolicyEntry> elements(){
765 return policyEntries.elements();
766 }
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823 public PermissionCollection getPermissions(final Subject subject,
824 final CodeSource codesource) {
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843 return java.security.AccessController.doPrivileged
844 (new java.security.PrivilegedAction<PermissionCollection>() {
845 public PermissionCollection run() {
846 SubjectCodeSource scs = new SubjectCodeSource
847 (subject,
848 null,
849 codesource == null ? null : codesource.getLocation(),
850 codesource == null ? null : codesource.getCertificates());
851 if (initialized)
852 return getPermissions(new Permissions(), scs);
853 else
854 return new PolicyPermissions(PolicyFile.this, scs);
855 }
856 });
857 }
858
859
860
861
862
863
864
865
866
867
868
869
870 PermissionCollection getPermissions(CodeSource codesource) {
871
872 if (initialized)
873 return getPermissions(new Permissions(), codesource);
874 else
875 return new PolicyPermissions(this, codesource);
876 }
877
878
879
880
881
882
883
884
885
886
887
888
889
890 Permissions getPermissions(final Permissions perms,
891 final CodeSource cs)
892 {
893 if (!initialized) {
894 init();
895 }
896
897 final CodeSource codesource[] = {null};
898
899 codesource[0] = canonicalizeCodebase(cs, true);
900
901 if (debug != null) {
902 debug.println("evaluate("+codesource[0]+")\n");
903 }
904
905
906
907
908
909 for (int i = 0; i < policyEntries.size(); i++) {
910
911 PolicyEntry entry = policyEntries.elementAt(i);
912
913 if (debug != null) {
914 debug.println("PolicyFile CodeSource implies: " +
915 entry.codesource.toString() + "\n\n" +
916 "\t" + codesource[0].toString() + "\n\n");
917 }
918
919 if (entry.codesource.implies(codesource[0])) {
920 for (int j = 0; j < entry.permissions.size(); j++) {
921 Permission p = entry.permissions.elementAt(j);
922 if (debug != null) {
923 debug.println(" granting " + p);
924 }
925 if (!addSelfPermissions(p, entry.codesource,
926 codesource[0], perms)) {
927
928
929
930
931 perms.add(p);
932 }
933 }
934 }
935 }
936
937
938
939 if (!ignoreIdentityScope) {
940 Certificate certs[] = codesource[0].getCertificates();
941 if (certs != null) {
942 for (int k=0; k < certs.length; k++) {
943 if ((aliasMapping.get(certs[k]) == null) &&
944 checkForTrustedIdentity(certs[k])) {
945
946
947
948
949 perms.add(new java.security.AllPermission());
950 }
951 }
952 }
953 }
954 return perms;
955 }
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973 private boolean addSelfPermissions(final Permission p,
974 CodeSource entryCs,
975 CodeSource accCs,
976 Permissions perms) {
977
978 if (!(p instanceof PrivateCredentialPermission))
979 return false;
980
981 if (!(entryCs instanceof SubjectCodeSource))
982 return false;
983
984
985 PrivateCredentialPermission pcp = (PrivateCredentialPermission)p;
986 SubjectCodeSource scs = (SubjectCodeSource)entryCs;
987
988
989 String[][] pPrincipals = pcp.getPrincipals();
990 if (pPrincipals.length <= 0 ||
991 !pPrincipals[0][0].equalsIgnoreCase("self") ||
992 !pPrincipals[0][1].equalsIgnoreCase("self")) {
993
994
995 return false;
996 } else {
997
998
999
1000
1001
1002 if (scs.getPrincipals() == null) {
1003
1004 return true;
1005 }
1006
1007 ListIterator<PolicyParser.PrincipalEntry> pli =
1008 scs.getPrincipals().listIterator();
1009 while (pli.hasNext()) {
1010
1011 PolicyParser.PrincipalEntry principal = pli.next();
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024 String[][] principalInfo = getPrincipalInfo
1025 (principal, accCs);
1026
1027 for (int i = 0; i < principalInfo.length; i++) {
1028
1029
1030
1031 PrivateCredentialPermission newPcp =
1032 new PrivateCredentialPermission
1033 (pcp.getCredentialClass() +
1034 " " +
1035 principalInfo[i][0] +
1036 " " +
1037 "\"" + principalInfo[i][1] + "\"",
1038 "read");
1039
1040 if (debug != null) {
1041 debug.println("adding SELF permission: " +
1042 newPcp.toString());
1043 }
1044
1045 perms.add(newPcp);
1046 }
1047 }
1048 }
1049 return true;
1050 }
1051
1052
1053
1054
1055
1056
1057
1058 private String[][] getPrincipalInfo
1059 (PolicyParser.PrincipalEntry principal,
1060 final CodeSource accCs) {
1061
1062
1063
1064
1065
1066
1067 if (!principal.principalClass.equals
1068 (PolicyParser.PrincipalEntry.WILDCARD_CLASS) &&
1069 !principal.principalName.equals
1070 (PolicyParser.PrincipalEntry.WILDCARD_NAME)) {
1071
1072
1073
1074 String[][] info = new String[1][2];
1075 info[0][0] = principal.principalClass;
1076 info[0][1] = principal.principalName;
1077 return info;
1078
1079 } else if (!principal.principalClass.equals
1080 (PolicyParser.PrincipalEntry.WILDCARD_CLASS) &&
1081 principal.principalName.equals
1082 (PolicyParser.PrincipalEntry.WILDCARD_NAME)) {
1083
1084
1085
1086
1087
1088
1089 SubjectCodeSource scs = (SubjectCodeSource)accCs;
1090
1091 Set<Principal> principalSet = null;
1092 try {
1093 Class pClass = Class.forName(principal.principalClass, false,
1094 ClassLoader.getSystemClassLoader());
1095 principalSet = scs.getSubject().getPrincipals(pClass);
1096 } catch (Exception e) {
1097 if (debug != null) {
1098 debug.println("problem finding Principal Class " +
1099 "when expanding SELF permission: " +
1100 e.toString());
1101 }
1102 }
1103
1104 if (principalSet == null) {
1105
1106 return new String[0][0];
1107 }
1108
1109 String[][] info = new String[principalSet.size()][2];
1110 java.util.Iterator<Principal> pIterator = principalSet.iterator();
1111
1112 int i = 0;
1113 while (pIterator.hasNext()) {
1114 Principal p = pIterator.next();
1115 info[i][0] = p.getClass().getName();
1116 info[i][1] = p.getName();
1117 i++;
1118 }
1119 return info;
1120
1121 } else {
1122
1123
1124
1125
1126
1127
1128 SubjectCodeSource scs = (SubjectCodeSource)accCs;
1129 Set<Principal> principalSet = scs.getSubject().getPrincipals();
1130
1131 String[][] info = new String[principalSet.size()][2];
1132 java.util.Iterator<Principal> pIterator = principalSet.iterator();
1133
1134 int i = 0;
1135 while (pIterator.hasNext()) {
1136 Principal p = pIterator.next();
1137 info[i][0] = p.getClass().getName();
1138 info[i][1] = p.getName();
1139 i++;
1140 }
1141 return info;
1142 }
1143 }
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158 Certificate[] getSignerCertificates(CodeSource cs) {
1159 Certificate[] certs = null;
1160 if ((certs = cs.getCertificates()) == null)
1161 return null;
1162 for (int i=0; i<certs.length; i++) {
1163 if (!(certs[i] instanceof X509Certificate))
1164 return cs.getCertificates();
1165 }
1166
1167
1168 int i = 0;
1169 int count = 0;
1170 while (i < certs.length) {
1171 count++;
1172 while (((i+1) < certs.length)
1173 && ((X509Certificate)certs[i]).getIssuerDN().equals(
1174 ((X509Certificate)certs[i+1]).getSubjectDN())) {
1175 i++;
1176 }
1177 i++;
1178 }
1179 if (count == certs.length)
1180
1181 return certs;
1182
1183 ArrayList<Certificate> userCertList = new ArrayList<>();
1184 i = 0;
1185 while (i < certs.length) {
1186 userCertList.add(certs[i]);
1187 while (((i+1) < certs.length)
1188 && ((X509Certificate)certs[i]).getIssuerDN().equals(
1189 ((X509Certificate)certs[i+1]).getSubjectDN())) {
1190 i++;
1191 }
1192 i++;
1193 }
1194 Certificate[] userCerts = new Certificate[userCertList.size()];
1195 userCertList.toArray(userCerts);
1196 return userCerts;
1197 }
1198
1199 private CodeSource canonicalizeCodebase(CodeSource cs,
1200 boolean extractSignerCerts) {
1201 CodeSource canonCs = cs;
1202 if (cs.getLocation() != null &&
1203 cs.getLocation().getProtocol().equalsIgnoreCase("file")) {
1204 try {
1205 String path = cs.getLocation().getFile().replace
1206 ('/',
1207 File.separatorChar);
1208 URL csUrl = null;
1209 if (path.endsWith("*")) {
1210
1211
1212 path = path.substring(0, path.length()-1);
1213 boolean appendFileSep = false;
1214 if (path.endsWith(File.separator))
1215 appendFileSep = true;
1216 if (path.equals("")) {
1217 path = System.getProperty("user.dir");
1218 }
1219 File f = new File(path);
1220 path = f.getCanonicalPath();
1221 StringBuffer sb = new StringBuffer(path);
1222
1223
1224
1225 if (!path.endsWith(File.separator) &&
1226 (appendFileSep || f.isDirectory()))
1227 sb.append(File.separatorChar);
1228 sb.append('*');
1229 path = sb.toString();
1230 } else {
1231 path = new File(path).getCanonicalPath();
1232 }
1233 csUrl = new File(path).toURL();
1234
1235 if (cs instanceof SubjectCodeSource) {
1236 SubjectCodeSource scs = (SubjectCodeSource)cs;
1237 if (extractSignerCerts) {
1238 canonCs = new SubjectCodeSource
1239 (scs.getSubject(),
1240 scs.getPrincipals(),
1241 csUrl,
1242 getSignerCertificates(scs));
1243 } else {
1244 canonCs = new SubjectCodeSource
1245 (scs.getSubject(),
1246 scs.getPrincipals(),
1247 csUrl,
1248 scs.getCertificates());
1249 }
1250 } else {
1251 if (extractSignerCerts) {
1252 canonCs = new CodeSource(csUrl,
1253 getSignerCertificates(cs));
1254 } else {
1255 canonCs = new CodeSource(csUrl,
1256 cs.getCertificates());
1257 }
1258 }
1259 } catch (IOException ioe) {
1260
1261
1262 if (extractSignerCerts) {
1263 if (!(cs instanceof SubjectCodeSource)) {
1264 canonCs = new CodeSource(cs.getLocation(),
1265 getSignerCertificates(cs));
1266 } else {
1267 SubjectCodeSource scs = (SubjectCodeSource)cs;
1268 canonCs = new SubjectCodeSource(scs.getSubject(),
1269 scs.getPrincipals(),
1270 scs.getLocation(),
1271 getSignerCertificates(scs));
1272 }
1273 }
1274 }
1275 } else {
1276 if (extractSignerCerts) {
1277 if (!(cs instanceof SubjectCodeSource)) {
1278 canonCs = new CodeSource(cs.getLocation(),
1279 getSignerCertificates(cs));
1280 } else {
1281 SubjectCodeSource scs = (SubjectCodeSource)cs;
1282 canonCs = new SubjectCodeSource(scs.getSubject(),
1283 scs.getPrincipals(),
1284 scs.getLocation(),
1285 getSignerCertificates(scs));
1286 }
1287 }
1288 }
1289 return canonCs;
1290 }
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337 private static class PolicyEntry {
1338
1339 CodeSource codesource;
1340 Vector<Permission> permissions;
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354 PolicyEntry(CodeSource cs)
1355 {
1356 this.codesource = cs;
1357 this.permissions = new Vector<Permission>();
1358 }
1359
1360
1361
1362
1363 void add(Permission p) {
1364 permissions.addElement(p);
1365 }
1366
1367
1368
1369
1370 CodeSource getCodeSource() {
1371 return this.codesource;
1372 }
1373
1374 public String toString(){
1375 StringBuffer sb = new StringBuffer();
1376 sb.append(rb.getString("LPARAM"));
1377 sb.append(getCodeSource());
1378 sb.append("\n");
1379 for (int j = 0; j < permissions.size(); j++) {
1380 Permission p = permissions.elementAt(j);
1381 sb.append(rb.getString("SPACE"));
1382 sb.append(rb.getString("SPACE"));
1383 sb.append(p);
1384 sb.append(rb.getString("NEWLINE"));
1385 }
1386 sb.append(rb.getString("RPARAM"));
1387 sb.append(rb.getString("NEWLINE"));
1388 return sb.toString();
1389 }
1390
1391 }
1392 }
1393
1394 class PolicyPermissions extends PermissionCollection {
1395
1396 private static final long serialVersionUID = -1954188373270545523L;
1397
1398 private CodeSource codesource;
1399 private Permissions perms;
1400 private PolicyFile policy;
1401 private boolean notInit;
1402 private Vector<Permission> additionalPerms;
1403
1404 PolicyPermissions(PolicyFile policy,
1405 CodeSource codesource)
1406 {
1407 this.codesource = codesource;
1408 this.policy = policy;
1409 this.perms = null;
1410 this.notInit = true;
1411 this.additionalPerms = null;
1412 }
1413
1414 public void add(Permission permission) {
1415 if (isReadOnly())
1416 throw new SecurityException
1417 (PolicyFile.rb.getString
1418 ("attempt.to.add.a.Permission.to.a.readonly.PermissionCollection"));
1419
1420 if (perms == null) {
1421 if (additionalPerms == null)
1422 additionalPerms = new Vector<Permission>();
1423 additionalPerms.add(permission);
1424 } else {
1425 perms.add(permission);
1426 }
1427 }
1428
1429 private synchronized void init() {
1430 if (notInit) {
1431 if (perms == null)
1432 perms = new Permissions();
1433
1434 if (additionalPerms != null) {
1435 Enumeration<Permission> e = additionalPerms.elements();
1436 while (e.hasMoreElements()) {
1437 perms.add(e.nextElement());
1438 }
1439 additionalPerms = null;
1440 }
1441 policy.getPermissions(perms,codesource);
1442 notInit=false;
1443 }
1444 }
1445
1446 public boolean implies(Permission permission) {
1447 if (notInit)
1448 init();
1449 return perms.implies(permission);
1450 }
1451
1452 public Enumeration<Permission> elements() {
1453 if (notInit)
1454 init();
1455 return perms.elements();
1456 }
1457
1458 public String toString() {
1459 if (notInit)
1460 init();
1461 return perms.toString();
1462 }
1463 }